﻿using System.Collections.Generic;
using System.Linq;
using Gola.VN.Scb.Web.Common.Entities;
using Gola.VN.Scb.Web.Dal;

namespace Gola.VN.Scb.Web.Bll
{
    internal class LocationBiz : BaseBiz
    {
        internal LocationBiz(MainDao mainDao)
            : base(mainDao)
        {

        }

        internal void InitializeData()
        {
            // 1. get data from database
            DaoPackage package = new DaoPackage();
            package.Channel = ChannelType.Sql;
            package.Event = DaoEvent.GetLocations;
            _mainDao.Execute(package);

            // 2. update data into memory
            package.Channel = ChannelType.Memory;
            package.Event = DaoEvent.SetLocations;
            package.Input = package.Output;
            _mainDao.Execute(package);
        }

        internal IEnumerable<LocationEntity> GetLocationByProviderId(int providerId)
        {
            DaoPackage package = new DaoPackage
                                     {
                                         Channel = ChannelType.Memory,
                                         Event = DaoEvent.GetLocations
                                     };

            _mainDao.Execute(package);
            var locations = package.Output as IEnumerable<LocationEntity>;
            if (locations == null)
            {
                return null;
            }

            return locations.Where(q => q.ProviderId == providerId);
        }
    }
}
